package mspdt.pocket.wallet;

import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class AccountInformation extends Activity{
	
	TextView account_name, account_amount, account_date, account_count;
	Button btn_add, btn_settings, btn_help;
	ListView account_list;
	CashAdapter adapter;
	DBAdapter db;
	int account_id;
	//ArrayList<String> accCategory, accAmount, accDescription, accPRG, accAccount, accDeb, accKre, accDate, accTime;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account_information);
		db = new DBAdapter(this);
		account_name = (TextView)findViewById(R.id.account_screen_account_name);
		account_amount = (TextView)findViewById(R.id.account_screen_amount);
		account_date = (TextView)findViewById(R.id.account_screen_date);
		account_count = (TextView)findViewById(R.id.account_screen_amount_count);
		
		btn_settings = (Button)findViewById(R.id.account_screen_settings);
		btn_add = (Button)findViewById(R.id.account_screen_add_amount);
		btn_help = (Button)findViewById(R.id.account_screen_help);
		
		account_list = (ListView)findViewById(R.id.account_listview);
		Bundle bundle = getIntent().getExtras();
		account_id = bundle.getInt("account_id");
		
		db.openDataBase();
		adapter = new CashAdapter(this, db.getAccountCategories(account_id), db.getAccountAmounts(account_id),
				db.getAccountDates(account_id), db.getAccountDescriptions(account_id));
		db.close();
		account_list.setAdapter(adapter);
		account_list.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				//Цаг тааруулах үйлдэл
				db.openDataBase();
				Date date = new Date(Long.parseLong(db.getAccountDates(account_id).get(arg2)));
				Time time = new Time(date.getTime());
				editActivity(db.getAccountAmounts(account_id).get(arg2), db.getAccountDescriptions(account_id).get(arg2), 
						String.valueOf(date.getYear()), String.valueOf(date.getMonth()), String.valueOf(date.getDate()), 
						String.valueOf(time.getHours()), String.valueOf(time.getMinutes()), db.getAccountPRG(account_id).get(arg2), 
						db.getGroups(account_id).get(arg2), db.getAccountCategories(account_id).get(arg2), account_id, 
						db.getAccountDEB(account_id).get(arg2), db.getAccountKRE(account_id).get(arg2));
				db.close();
				return false;
			}
		});
		
		btn_add.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				editActivity("", "", "", "", "", "", "", "", 0, "", 0, "", "");
			}
		});		
	}
	public void editActivity(String amount, String description, String year, String month, String day, String hour, String minute,
			String prg, int group, String category, int account, String deb, String kre)
	{
		Bundle bundle = new Bundle();
		
		Intent edit_activity = new Intent(getApplicationContext(), EditActivity.class);
		
		bundle.putString("amount", amount);
		bundle.putString("description", description);
		bundle.putString("year", year);
		bundle.putString("month", month);
		bundle.putString("day", day);
		bundle.putString("hour", hour);
		bundle.putString("minute", minute);
		bundle.putString("prg", prg);
		bundle.putInt("group", group);
		bundle.putString("category", category);
		bundle.putInt("account", account);
		bundle.putString("deb_str", deb);
		bundle.putString("kre_str", kre);
		
		edit_activity.putExtras(bundle);
		startActivity(edit_activity);
	}

}
